#!/usr/bin/python3
# coding=utf-8
import sys
import json
import random

def iosdiagJoinData(raw):
    if raw.startswith('fail'):
        data = {"status":"fail"}
    elif 'summary' not in raw:
        data = {"status":"success","IO timeout":"false"}
    else:
        data = {"status":"success","IO timeout":"true","stat":[],"seq":[]}
        for s in raw.split('\n'):
            obj = json.loads(s)
            if "percent" in str(obj):
                data['stat'] = obj['summary']
            else:
                data["seq"] = obj['summary']
        for diskIdx in range(len(list(data["seq"]))):
            seq = data["seq"][diskIdx]
            for idx in range(len(list(seq["slow ios"]))-1, 0, -1):
                if seq["slow ios"][idx]["time"] == seq["slow ios"][idx-1]["time"]:
                    if seq["slow ios"][idx]["totaldelay"] <= seq["slow ios"][idx-1]["totaldelay"]:
                        del seq["slow ios"][idx]
                    else:
                        del seq["slow ios"][idx-1]
    s = json.dumps(data, indent=4)
    print(s)

def extract_params():
    path, res, task_id = sys.argv[1], "", sys.argv[2]
    with open(path, 'r') as tmp:
        res = tmp.read()
    return res, task_id

if __name__ == "__main__":
    res, _ = extract_params()
    iosdiagJoinData(res)
